//
//  MainViewController.swift
//  weibo
//
//  Created by 王、小胜 on 16/7/23.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    
    //Mark: - 按钮的监听方法
    @objc private func clickComposedButton(){
        
       print("点我了")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //添加控制器
        addChildViewControllers()
        //添加按钮
        setupComposedButton()
    }
    
    override func viewWillAppear(animated: Bool) {
        //会创建tababr中所有的按钮
        super.viewWillAppear(animated)
        
        //将撰写按钮写到最前面
        tabBar.bringSubviewToFront(composedButton)
    }
    
    //MARK :-懒加载控件
    
    private lazy var composedButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
//    {
//        //自定义button样式
//        let button = UIButton()
//          button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
////        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlight"), forState: UIControlState.Highlighted)
////        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
////        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlight"), forState: UIControlState.Highlighted)
////        //根据图片大小调整尺寸
////        button.sizeToFit()
//        return button
//        }()
}


// MARK :-设置界面

extension MainViewController {
    
    ///设置撰写按钮
    private func setupComposedButton() {
        //1.添加按钮
        tabBar.addSubview(composedButton)
        
        //2.调整按钮
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 1
        
        composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
        //3.添加监听方法
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: .TouchUpInside)
    }
    
    
    
    //添加所有控制器
    private func addChildViewControllers(){
        //设置渲染 tintcolor  -图片渲染颜色
        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    //添加控制器
    private func addChildViewController(vc: UIViewController, title:String ,imageName: String) {
        //设置标题
        vc.title = title
        //设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        //导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
    
}


